package com.servlet;

import com.entity.Users;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Enumeration;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Created by Wuxiang on 2018/4/10
 */
public class RegServlet extends HttpServlet{

    /**
     * Constructor of the object.
     */
    public RegServlet() {
        System.out.println("Servlet 构造函数......");
    }

    /**
     * Destruction of the servlet. <br>
     */
    public void destroy() {
        System.out.println("Servlet 销毁......");
        super.destroy();
    }

    /**
     * The doGet method of the servlet. <br>
     *
     * This method is called when a form has its tag value method equals to get.
     *
     * @param request the request send by the client to the server
     * @param response the response send by the server to the client
     * @throws ServletException if an error occurred
     * @throws IOException if an error occurred
     */
    public void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

        doPost(request,response);
    }

    /**
     * The doPost method of the servlet. <br>
     *
     * This method is called when a form has its tag value method equals to post.
     *
     * @param request the request send by the client to the server
     * @param response the response send by the server to the client
     * @throws ServletException if an error occurred
     * @throws IOException if an error occurred
     */
    public void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

        request.setCharacterEncoding("utf-8");

        System.out.println("------所有参数------");
        Enumeration<String> enumeration = request.getParameterNames();
        while (enumeration.hasMoreElements()){
            System.out.println(enumeration.nextElement());
        }
        System.out.println("------所有参数------");

        Users u = new Users();
        String username,mypassword,gender,email,introduce,flag;
        Date birthday;
        String[] favorites;

        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        try
        {
            username = request.getParameter("username");
            mypassword = request.getParameter("mypassword");
            gender = request.getParameter("gender");
            email = request.getParameter("email");
            introduce = request.getParameter("introduce");
            birthday = sdf.parse(request.getParameter("birthday"));

            if (request.getParameterValues("flag") != null) {
                flag = request.getParameter("flag");
            } else {
                flag = "false";
            }
            //用来获取多个复选按钮的值
            favorites = request.getParameterValues("favorite");
            u.setUsername(username);
            u.setMypassword(mypassword);
            u.setGender(gender);
            u.setEmail(email);
            u.setFavorites(favorites);
            u.setIntroduce(introduce);
            if(flag.equals("true")) {
                u.setFlag(true);
            } else {
                u.setFlag(false);
            }
            u.setBirthday(birthday);

            //把注册成功的用户对象保存在session中
            request.getSession().setAttribute("regUser", u);
            //跳转到注册成功页面
            request.getRequestDispatcher("../userinfo.jsp").forward(request,response);
        }
        catch(Exception ex)
        {
            ex.printStackTrace();
        }


    }

    /**
     * Initialization of the servlet. <br>
     *
     * @throws ServletException if an error occurs
     */
    public void init() throws ServletException {
        System.out.println("Servlet init......");
    }
}
